package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.UserState;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserCommonService {
    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private UserInfoBindService userInfoBindService;

    @Autowired
    protected UserInfoService userInfoService;

    @Autowired
    protected UserRealInfoService userRealInfoService;

    @Autowired
    protected UserBankCardService userBankCardService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected UserCapitalService userCapitalService;

    @Autowired
    protected UserTransferService userTransferService;

    @Autowired
    protected UserDealHistoryService userDealHistoryService;

    @Autowired
    protected HangBuyService hangBuyService;

    @Autowired
    protected HangSaleService hangSaleService;

    @Autowired
    protected MemberWalletService memberWalletService;

    @Autowired
    protected ShareholderWalletService shareholderWalletService;

    @Autowired
    protected ReturnMoneyService returnMoneyService;

    @Autowired
    protected FeeService feeService;

    @Autowired
    protected UserRoleService userRoleService;

    @Autowired
    protected JedisUtil jedisUtil;

    @Autowired
    protected UserWithdrawalCashService userWithdrawalCashService;

    public List<UserCompletedInfo> searchByRealName(String name) {
        List<UserRealInfo> userRealInfos = userRealInfoService.getsByRealName(name);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(userRealInfos.size());
        if (userRealInfos == null || userRealInfos.isEmpty()) {
        } else {
            for (UserRealInfo userRealInfo : userRealInfos) {
                UserCompletedInfo userCompletedInfo = castUserRealInfoToUserCompletedInfo(userRealInfo);
                if (userCompletedInfo != null) {
                    userCompletedInfoList.add(userCompletedInfo);
                }
            }
        }
        return userCompletedInfoList;
    }

    public UserCompletedInfo searchByPhoneNum(String phoneNum) {
        UserRealInfo userRealInfo = userRealInfoService.getByPhoneNum(phoneNum);
        return castUserRealInfoToUserCompletedInfo(userRealInfo);
    }

    public UserCompletedInfo searchByIdCardNum(String idCardNum) {
        UserRealInfo userRealInfo = userRealInfoService.getByIdCardNum(idCardNum);
        return castUserRealInfoToUserCompletedInfo(userRealInfo);
    }


    public List<UserCompletedInfo> searchByRegistDate(LocalDateTime lowDate, LocalDateTime highDate) {
        List<User> users = userService.searchByRegistDate(lowDate, highDate);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(users.size());
        for (User user : users) {
            userCompletedInfoList.add(castUserToUserCompletedInfo(user));
        }
        return userCompletedInfoList;
    }

    public List<UserCompletedInfo> searchByRegistDate(LocalDate date) {
        List<User> users = userService.searchByRegistDate(date);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(users.size());
        for (User user : users) {
            userCompletedInfoList.add(castUserToUserCompletedInfo(user));
        }
        return userCompletedInfoList;
    }


    public List<UserCompletedInfo> searchBySleepCoinCount(Integer coinTypeId, BigDecimal low, BigDecimal high) {
        List<UserCapital> userCapitals = userCapitalService.searchBySleepCoin(coinTypeId, low, high);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(userCapitals.size());
        for (UserCapital userCapital : userCapitals) {
            userCompletedInfoList.add(castUserCapitalToUserCompletedInfo(userCapital));
        }
        return userCompletedInfoList;
    }

    public List<UserCompletedInfo> searchByAliveCoinCount(Integer coinTypeId, BigDecimal low, BigDecimal high) {
        List<UserCapital> userCapitals = userCapitalService.searchByAliveCoin(coinTypeId, low, high);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(userCapitals.size());
        for (UserCapital userCapital : userCapitals) {
            userCompletedInfoList.add(castUserCapitalToUserCompletedInfo(userCapital));
        }
        return userCompletedInfoList;
    }

    public List<UserCompletedInfo> searchByState(Integer state) {
        List<User> users = userService.searchByState(state);
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>(users.size());
        for (User user : users) {
            userCompletedInfoList.add(castUserToUserCompletedInfo(user));
        }
        return userCompletedInfoList;
    }

    public void unblocking(Integer userId) {
        User user = userService.getByPrimaryKey(userId);
        user.setState(0);
        userService.update(user);

    }


    protected UserCompletedInfo castUserCapitalToUserCompletedInfo(UserCapital userCapital) {
        return createUserCompletedInfo(userCapital.getUserId());
    }

    protected UserCompletedInfo castUserRealInfoToUserCompletedInfo(UserRealInfo userRealInfo) {
        if (userRealInfo == null) {
        } else {
            return createUserCompletedInfo(userRealInfo.getId());
        }
        return null;
    }

    protected UserCompletedInfo castUserToUserCompletedInfo(User user) {
        return createUserCompletedInfo(user.getId());
    }

    protected UserCompletedInfo createUserCompletedInfo(Integer userId) {
        UserCompletedInfo userCompletedInfo = new UserCompletedInfo();
        User user = userService.getByPrimaryKey(userId);
        Integer state = user.getState();
        String userStateName = "";
        if (state == UserState.OK) {
            userStateName = "正常";
        } else if (state == UserState.FROZEN) {
            userStateName = "钱包冻结";
        } else if (state == UserState.CLOSED) {
            userStateName = "封号";
        } else if (state == UserState.ACTIVATING) {
            userStateName = "未激活";
            if (user.getType() == 1) {
                userStateName = "激活中";
            }
        }

//        boolean b = userRoleService.hasRole(userId, UserType.NONE);
//        Integer type = user.getType();
//        if (b){
//            userStateName = "未激活";
//        }

        user.setUserStateName(userStateName);
        userCompletedInfo.setUser(user);
        userCompletedInfo.setUserBankCardList(userBankCardService.getsByUserId(userId));
        List<UserCapital> userCapitals = userCapitalService.getsByUserIdAndType(userId, UserType.MEMBER);

        for (UserCapital userCapital : userCapitals) {
            userCapital.setTotalDealPrice(userDealHistoryService.getTotalDealPrice(userId));
        }
        userCompletedInfo.setUserCapitalList(userCapitals);
        userCompletedInfo.setUserInfo(userInfoService.getByPrimaryKey(userId));
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        if (userRealInfo == null)
            userCompletedInfo.setUserRealInfo(null);
        else {

            String idCardNum = userRealInfo.getIdCardNum();
            userRealInfo.setIdCardNum(idCardNum.substring(0, 4) + "****" + idCardNum.substring(idCardNum.length() - 4));
            userCompletedInfo.setUserRealInfo(userRealInfo);

        }
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        String phoneNum = userInfoBind.getPhoneNum();
        userInfoBind.setSmsPhoneNum(phoneNum);
        userCompletedInfo.setUserInfoBind(userInfoBind);
        userCompletedInfo.setUserDealHistoryList(userDealHistoryService.getsByUserId(userId));
        userCompletedInfo.setTotalTransferCount(userTransferService.getTotalTransByUserId(userId));
        userCompletedInfo.setThisMonthTransferCount(userTransferService.getThisMonthCountByUserId(userId));

        /** 完善UserCompletedInfo的子类对象 **/
        complete(userCompletedInfo);
        return userCompletedInfo;
    }

    /**
     * 交由子类重写
     *
     * @param info
     */
    protected void complete(UserCompletedInfo info) {

        if (info == null)
            return;
        User user = info.getUser();
        UserRealInfo userRealInfo = info.getUserRealInfo();
        UserInfoBind userInfoBind = info.getUserInfoBind();
        List<UserDealHistory> userDealHistoryList = info.getUserDealHistoryList();
        List<UserBankCard> userBankCardList = info.getUserBankCardList();
        List<UserCapital> userCapitalList = info.getUserCapitalList();

        //用户姓名
        String userRealName = "";
        if (userRealInfo == null) {
            info.setUserRealName(userRealName);
        } else {
            info.setUserRealName(userRealInfo.getRealName());
        }
        //手机号
        String phoneNum = "";
        if (user.getRegistType() == 0) {
            info.setPhoneNum(user.getUsername());
        } else {
            if (StringUtils.hasText(userInfoBind.getPhoneNum())) {
                info.setPhoneNum(userInfoBind.getPhoneNum());
            } else {
                info.setPhoneNum(phoneNum);
            }
        }
        //累计转账
        BigDecimal totalTransferCount = new BigDecimal(0);
        List<UserTransfer> userTransfers = userTransferService.getsUserTransfer(user.getId());
        if (userTransfers == null || userTransfers.isEmpty()) {
            info.setTotalTransferCount(totalTransferCount);
        } else {
            for (UserTransfer userTransfer : userTransfers) {
                totalTransferCount = totalTransferCount.add(userTransfer.getTransferCount());
            }
        }
        //累计交易数量,累计交易金额
        BigDecimal totalDealCount = new BigDecimal(0);
        BigDecimal totalDeal = new BigDecimal(0);
        if (userDealHistoryList == null || userDealHistoryList.isEmpty()) {
            info.setTotalDealCountOne(totalDealCount);
            info.setTotalDealPrice(totalDeal);
        } else {
            for (UserDealHistory userDealHistory : userDealHistoryList) {
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                BigDecimal singlePrice = userDealHistory.getSinglePrice();
                totalDealCount = totalDealCount.add(dealAliveCoinCount);
                BigDecimal multiply = dealAliveCoinCount.multiply(singlePrice);
                totalDeal = totalDeal.add(multiply);
            }
            info.setTotalDealCountOne(totalDealCount);
            info.setTotalDealPrice(totalDeal);
        }
        //totalAliveCount 货币持有量
        BigDecimal totalAliveCount = new BigDecimal(0);
        if (userCapitalList == null || userCapitalList.isEmpty()) {
            info.setTotalAliveCount(totalAliveCount);
        } else {
            for (UserCapital userCapital : userCapitalList) {
                totalAliveCount = totalAliveCount.add(userCapital.getAliveCoinCount());
            }
            info.setTotalAliveCount(totalAliveCount);
        }
    }

    public User register(String username, String password, Integer registerType) {
        return userService.insert(username, password, registerType);
    }


    public UserCompletedInfo searchByUserId(Integer userId) {
        User user = userService.getByPrimaryKey(userId);
        return castUserToUserCompletedInfo(user);
    }

    public BigDecimal getTotalTransferByUserType(Integer userType) {
        List<User> userList = userService.getsByUserType(userType);
        BigDecimal totalTransfer = new BigDecimal(0);
        for (User user : userList) {
            BigDecimal totalTransByUserId = userTransferService.getTotalTransByUserId(user.getId());
            totalTransfer = totalTransfer.add(totalTransByUserId);
        }
        return totalTransfer;
    }

    public List<UserTransfer> getTransferHistoryByUserType(Integer userType) {
        List<User> userList = userService.getsByUserType(userType);
        ArrayList<UserTransfer> userTransfers = new ArrayList<>();
        for (User user : userList) {
            Integer id = user.getId();
            List<UserTransfer> userTransfer = userTransferService.getsUserTransfer(id);
            for (UserTransfer transfer : userTransfer) {
                Integer senderId = transfer.getSenderId();
                transfer.setSenderName(userRealInfoService.getByPrimaryKey(senderId).getRealName());
                Integer receiverId = transfer.getReceiverId();
                UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(receiverId);
                if (userRealInfo == null) {
                    transfer.setReceiverName("账号" + userService.getByPrimaryKey(receiverId).getUsername());
                } else {
                    transfer.setReceiverName(userRealInfo.getRealName());
                }
            }
            userTransfers.addAll(userTransfer);
        }
        return userTransfers;
    }


    public BigDecimal getMonthTransferByUserType(Integer userType) {
        List<User> userList = userService.getsByUserType(userType);
        BigDecimal monthTransfer = new BigDecimal(0);

        if (userList == null || userList.isEmpty()) {
            return monthTransfer;
        } else {
            for (User user : userList) {
                BigDecimal monthTransferByUserId = userTransferService.getMonthTransferByUserId(user.getId());
                monthTransfer = monthTransfer.add(monthTransferByUserId);
            }
            return monthTransfer;
        }

    }

    /**
     * 销户
     *
     * @param id 用户id
     */
    public void delete(Integer id, int type) {
        returnMoney(id);
        UserCompletedInfo userCompletedInfo = createUserCompletedInfo(id);
        if (userRoleService.getsByUserId(id).size() < 2) {
            userService.delete(id);

            UserInfoBind userInfoBind = userCompletedInfo.getUserInfoBind();
            userInfoBindService.delete(userInfoBind.getId());

            UserInfo userInfo = userCompletedInfo.getUserInfo();
            userInfoService.delete(userInfo.getId());

            UserRealInfo userRealInfo = userCompletedInfo.getUserRealInfo();
            userRealInfoService.delete(userRealInfo.getId());

            List<UserBankCard> userBankCardList = userCompletedInfo.getUserBankCardList();
            for (UserBankCard userBankCard : userBankCardList) {
                userBankCardService.delete(userBankCard.getId());
            }

            List<UserDealHistory> userDealHistoryList = userCompletedInfo.getUserDealHistoryList();
            for (UserDealHistory userDealHistory : userDealHistoryList) {
                userDealHistoryService.delete(userDealHistory.getId());
            }

            List<UserTransfer> userTransferList = userTransferService.getsByUserId(id);
            for (UserTransfer userTransfer : userTransferList) {
                userTransferService.delete(userTransferList);
            }

        }


        List<UserCapital> userCapitalList = userCapitalService.getsByUserIdAndUserType(id, type);

        for (UserCapital userCapital : userCapitalList) {
            userCapitalService.delete(userCapital.getId());
        }

        UserRole userRole = userRoleService.getByUserIdAndUserRole(id, type);
        userRoleService.delete(userRole.getId());


    }

    /**
     * 销户退钱
     *
     * @param userId
     */
    private void returnMoney(Integer userId) {
        BigDecimal returnMoneyCount = new BigDecimal(0);

        User user = userService.getByPrimaryKey(userId);
        if (user.getType() == UserType.MEMBER) {
            Integer hangBuyId = hangBuyService.getDealingByUserId(userId).getId();
            hangBuyService.cancel(hangBuyId);

            Integer hangSaleId = hangSaleService.getDealingByUserId(userId).getId();
            hangSaleService.cancel(hangSaleId);

            MemberWallet memberWallet = memberWalletService.getByMemberId(userId);
            returnMoneyCount = returnMoneyCount.add(memberWallet.getUserCashWallet().getCashCount());
            List<UserCapital> userCapitalList = memberWallet.getUserCapitalList();
            for (UserCapital userCapital : userCapitalList) {
                Integer coinTypeId = userCapital.getCoinTypeId();
                //Jedis //jedis = jedisUtil.getJedis();
                BigDecimal openPrice = new BigDecimal(jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId)));
                //jedisUtil.close();
                returnMoneyCount = returnMoneyCount.add(openPrice.multiply(userCapital.getAliveCoinCount()));
                returnMoneyCount = returnMoneyCount.add(openPrice.multiply(userCapital.getSleepCoinCount()));
            }

        }
        if (user.getType() == UserType.SHAREHOLDER) {
            ShareholderWallet shareholderWallet = shareholderWalletService.getByShareholderId(userId);
            CashWallet shareholderCashWallet = shareholderWallet.getShareholderCashWallet();
            returnMoneyCount = returnMoneyCount.add(shareholderCashWallet.getCashCount());

            List<UserCapital> userCapitalList = shareholderWallet.getUserCapitalList();
            for (UserCapital userCapital : userCapitalList) {
                Integer coinTypeId = userCapital.getCoinTypeId();
                //Jedis //jedis = jedisUtil.getJedis();
                BigDecimal openPrice = new BigDecimal(jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId)));
                //jedisUtil.close();
                returnMoneyCount = returnMoneyCount.add(openPrice.multiply(userCapital.getAliveCoinCount()));
                returnMoneyCount = returnMoneyCount.add(openPrice.multiply(userCapital.getSleepCoinCount()));
            }

        }

        /** 查询手续费率 **/
        //Jedis //jedis = jedisUtil.getJedis();
        BigDecimal returnMoneyFeeRatio = new BigDecimal(jedisUtil.get(RedisKeyCreator.returnMoneyFeeRatio()));
        //jedisUtil.close();

        /** 扣除手续费 **/
        returnMoneyCount = returnMoneyCount.subtract(returnMoneyCount.multiply(returnMoneyFeeRatio));

        ReturnMoney returnMoney = new ReturnMoney();
        returnMoney.setId(userId);
        returnMoney.setReturnMoney(returnMoneyCount);
        returnMoneyService.insert(returnMoney);
    }

    public List<User> getNoDealUserByMonth(Integer month) {
        List<User> users = userService.getAll();
        ArrayList<User> list = new ArrayList<>();
        for (User user : users) {
            Integer id = user.getId();
            List<UserDealHistory> historyByUserIdAndMonth = userDealHistoryService.getHistoryByUserIdAndMonth(id, month);
            LocalDateTime lastDealTime = userDealHistoryService.getLastDealTime(id);
            user.setLastDealTime(lastDealTime);

            UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(id);
            if (userRealInfo == null) {
                user.setUserRealName(user.getUsername());
            } else {
                user.setUserRealName(userRealInfo.getRealName());
            }
            if (historyByUserIdAndMonth == null || historyByUserIdAndMonth.isEmpty()) {
                list.add(user);
            }
        }
        return list;
    }

    public List<User> getNoDealUserByDay(Integer day) {
        List<User> users = userService.getAll();
        ArrayList<User> list = new ArrayList<>();
        for (User user : users) {
            Integer id = user.getId();
            List<UserDealHistory> historyByUserIdAndMonth = userDealHistoryService.getHistoryByUserIdAndDay(id, day);
            LocalDateTime lastDealTime = userDealHistoryService.getLastDealTime(id);
            user.setLastDealTime(lastDealTime);

            UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(id);
            if (userRealInfo == null) {
                user.setUserRealName(user.getUsername());
            } else {
                user.setUserRealName(userRealInfo.getRealName());
            }

            if (historyByUserIdAndMonth == null || historyByUserIdAndMonth.isEmpty()) {
                list.add(user);
            }
        }
        return list;
    }

    public Integer getTotalSealupCount() {
        return userService.getsByUserState(UserState.CLOSED).size();
    }

    public Integer getTotalFrozenCount() {
        return userService.getsByUserState(UserState.FROZEN).size();
    }


    public List<User> getByTodayAndUserState(Integer userState) {
        TimeExample example = new TimeExample(User.class);
        example.equals("state", userState);
        LocalDateTime localDateTime = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTimeInZero());
        example.timeGreaterThan("updateTime", localDateTime);
        return userService.getsByExample(example);
    }

    public List<UserCompletedInfo> searchMByNameAndPhoneNumAndIdCardNum(String userName, String phoneNum, String idCardNum, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<UserCompletedInfo> list = new ArrayList<>();
        List<Integer> userIdByUserName = getUserIdByUserName(userName);
        if (phoneNum != null) {
            Integer userIdByPhoneNum = getUserIdByPhoneNum(phoneNum);
            if (userIdByPhoneNum != null) {
                list.add(searchByUserId(userIdByPhoneNum));
            }
        }

        if (idCardNum != null) {
            Integer userIdByIdCardNum = getUserIdByIdCardNum(idCardNum);
            list.add(searchByUserId(userIdByIdCardNum));
        }

        if (userIdByUserName != null || (!userIdByUserName.isEmpty())) {
            for (Integer integer : userIdByUserName) {
                list.add(searchByUserId(integer));
            }
        }
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                Integer idi = list.get(j).getUser().getId();
                Integer idj = list.get(i).getUser().getId();
                if (idi == idj) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    public List<Integer> getUserIdByUserName(String userName) {
        List<UserRealInfo> userRealInfos = userRealInfoService.getsByRealName(userName);
        ArrayList<Integer> list = new ArrayList<>();
        if (userRealInfos == null || userRealInfos.isEmpty()) {
        } else {
            for (UserRealInfo userRealInfo : userRealInfos) {
                Integer id = userRealInfo.getId();
                list.add(id);
            }
        }
        return list;
    }

    public Integer getUserIdByPhoneNum(String phoneNum) {
        UserInfoBind userInfoBind = userInfoBindService.getByPhoneNum(phoneNum);
        if (userInfoBind == null) {
            return null;
        } else {
            return userInfoBind.getId();
        }
    }

    public Integer getUserIdByIdCardNum(String idCardNum) {
        UserRealInfo userRealInfo = userRealInfoService.getByIdCardNum(idCardNum);
        if (userRealInfo == null) {
            return null;
        } else {
            return userRealInfo.getId();
        }
    }

    public BaseDto search(int pageNum,
                          int pageSize,
                          String realName,
                          String phoneNum,
                          String idCardNum,
//                          Integer lowLevel,
//                          Integer highLevel,
                          int coinTypeId,
                          LocalDateTime startRegisterTime,
                          LocalDateTime endRegisterTime,
                          BigDecimal minDealedCount,
                          BigDecimal maxDealedCount,
                          BigDecimal minAliveCoinCount,
                          BigDecimal maxAliveCoinCount,
                          Integer memberState) {
        List<UserCompletedInfo> userCompletedInfoList = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();
        List<Integer> memberIds = userRoleService.getsByUserRole(UserType.MEMBER);
        List<Integer> id = userRoleService.getsByUserRole(UserType.NONE);
        memberIds.addAll(id);

        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> userRealInfoParamMap = new HashMap<>();
        boolean b = false;
        if (StringUtils.hasText(realName)) {
            userRealInfoParamMap.put("realName", realName);
            b = true;
        }
        if (StringUtils.hasText(phoneNum)) {
            userRealInfoParamMap.put("phoneNum", phoneNum);
            b = true;
        }
        if (StringUtils.hasText(idCardNum)) {
            userRealInfoParamMap.put("idCardNum", idCardNum);
            b = true;
        }
        if (b) {
            List<UserRealInfo> userRealInfoList = userRealInfoService.getsWithoutComplete(userRealInfoParamMap);
            if (userRealInfoList != null && !userRealInfoList.isEmpty()) {
                userRealInfoList.forEach((x) -> {
                    ids.add(x.getId());
                });
            }
            memberIds.retainAll(ids);
            ids.clear();
        }


//        if (lowLevel != null && highLevel != null) {
//            List<ShareholderLevel> shareholderLevelList = shareholderLevelService.searchByLevel(lowLevel, highLevel);
//            shareholderLevelList.forEach((x) -> {
//                memberIdSet.add(x.getShareholderId());
//            });
//        }
        boolean registerTime = false;
        if (startRegisterTime != null && endRegisterTime != null) {
            List<User> userList = userService.searchByRegistDate(startRegisterTime, endRegisterTime);
            userList.forEach((x) -> {
                ids.add(x.getId());
            });
            registerTime = true;
        }
        if (registerTime) {
            memberIds.retainAll(ids);
            ids.clear();
        }


        CommonExample example = new CommonExample(UserCapital.class);
        boolean flag = false;

        example.equals("coinTypeId", coinTypeId);
        if (minDealedCount != null && maxDealedCount != null) {
            example.between("totalDealCount", minDealedCount, maxDealedCount);
            flag = true;
        }
        if (minAliveCoinCount != null && maxAliveCoinCount != null) {
            example.between("aliveCoinCount", minAliveCoinCount, maxAliveCoinCount);
            flag = true;
        }

        if (flag) {
            List<UserCapital> userCapitalList = userCapitalService.getsByExampleWithoutComplete(example);
            userCapitalList.forEach((x) -> {
                ids.add(x.getUserId());
            });
            memberIds.retainAll(ids);
            ids.clear();
        }


        if (memberState != null) {
            CommonExample example1 = new CommonExample(User.class);
            example1.equals("type", UserType.MEMBER);
            example.equals("state", memberState);
            List<UserRole> userRoleList = userRoleService.getsByExampleWithoutComplete(example);
            userRoleList.forEach((x) -> {
                ids.add(x.getId());
            });
            memberIds.retainAll(ids);
            ids.clear();
        }

        for (Integer memberId : memberIds) {
            UserCompletedInfo userCompletedInfo = this.searchByUserId(memberId);
            userCompletedInfoList.add(userCompletedInfo);
        }
        BaseDto baseDto = new BaseDto();
        PageHelper.startPage(pageNum, pageSize);
        baseDto.setData(new PageInfo<UserCompletedInfo>(userCompletedInfoList));
        return baseDto;

    }

    public ShareholderDetail memberShareholderDetail(Integer userId, int coinTypeId) {
        ShareholderDetail memberDetail = new ShareholderDetail();
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeIdAndUserType(userId, coinTypeId, UserType.MEMBER);
        CoinType coinType = coinTypeService.getByPrimaryKey(coinTypeId);
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        if (userRealInfo != null) {
            memberDetail.setUserRealName(userRealInfo.getRealName());
            memberDetail.setCardTypeName(userRealInfo.getCardTypeName());
            memberDetail.setIdCardNum(userRealInfo.getIdCardNum());
        } else {
            memberDetail.setUserRealName("");
            memberDetail.setCardTypeName("");
            memberDetail.setIdCardNum("");
        }
        if (userCapital != null) {
            memberDetail.setAliveCoinCount(userCapital.getAliveCoinCount());
            memberDetail.setSleepCoinCount(userCapital.getSleepCoinCount());
        } else {
            memberDetail.setSleepCoinCount(new BigDecimal(0));
            memberDetail.setAliveCoinCount(new BigDecimal(0));
        }
        memberDetail.setCoinTypeName(coinType.getName());
        memberDetail.setCoinTypeId(coinTypeId);
        memberDetail.setPhoneNum(userInfoBind.getPhoneNum());
        return memberDetail;
    }
}
